/**
 * Created by rongyao on 2017/10/31.
 */
export default {
	data () {
		return {
			cateId:{},
			filter:{
				rows:10,
				data: [],
				page:1,
				total:0,
				username:"",
				keyWords:"",
				ids:"",
				cateId:"",
				title:"",
				content:""
			},
			modal:false,
			formValidate:{
				username:"",
				password:"",
				age:"",
				address:"",
				gender: '',
				comment:"",
				commentator:"",
				pic:[],
				name:"",
				describe:""
			},
		}
	},
	methods: {
		show (row) {
			Object.assign(this.formValidate,row);
			this.path="http://localhost:3000/"+this.formValidate.pic.filename
			this.modal=true;
			console.log(row);
		},
		onAdd(){
			if(this.type&&(this.type==1||this.type==2||this.type==3)){
				if(this.cateId._id&&this.cateId._id.length>0){
					this.formValidate = this.$options.data().formValidate;
					this.paths=[];
					this.modal=true
				}else{
					this.$Message.info('请选择要添加新闻的分类')
				}
			}else{
				this.formValidate = this.$options.data().formValidate;
				this.modal=true
			}
			
		},
		remove (id) {
			this.$Modal.confirm({
				title: '数据操作',
				content: '<p>确认删除数据吗</p>',
				onOk: () => {
					this.$http.delete(`http://localhost:3000/${this.module}/data/${id}`).then(res=>{
						this.$Message.info('数据删除成功');
						this.getData();
					})
				},
				onCancel: () => {
//						this.$Message.info('点击了取消');
				}
			});
			
			
		},
		onChange(index){
			console.log(index);
			this.filter.page=index;
			this.getData();
		},
		onPageSizeChange(pageSize){
			this.filter.rows=pageSize;
			this.getData();
		},
		searchData(){
			this.getData();
		},
		getData(){
			// console.log(this.filter);
			this.$http.post(`http://localhost:3000/${this.module}/list`,this.filter).then(res=>{
				this.filter.data =  res.data.rows;
				this.filter.total =  res.data.total;
			});
		},
		onSelectionChange(rows){
			var ids=[];
			for(var index in rows){
				ids.push(rows[index]._id);
			}
			this.filter.ids=ids.toString();
			console.log(this.filter.ids);
		},
		deleteSelection(){
			this.$Modal.confirm({
				title: '数据操作',
				content: '<p>确认删除数据吗</p>',
				onOk: () => {
					this.$http.post(`http://localhost:3000/${this.module}/remove`,{ids:this.filter.ids}).then(res=>{
						this.$Message.info('数据删除成功');
						this.getData();
					})
				},
				onCancel: () => {
//						this.$Message.info('点击了取消');
				}
			});
		},
		handleSubmit (name) {
			this.$refs[name].validate((valid) => {
				if (valid) {
					if(this.formValidate._id){
						console.log(this.formValidate._id);
						this.$http.post(`http://localhost:3000/${this.module}/data/${this.formValidate._id}`,this.formValidate).then(res=>{
							this.$Message.success('修改成功!');
							this.modal=false;
							this.getData()
						})
					}else{
						if(this.type&&(this.type==1||this.type==2||this.type==3)){
							this.formValidate.cateId = this.cateId._id;
						}
						if(this.filter.cateId&&this.filter.cateId.length>0){
							this.formValidate.cateId = this.filter.cateId
						}
						this.$http.post(`http://localhost:3000/${this.module}/data`,this.formValidate).then(res=>{
							this.$Message.success('提交成功!');
							this.modal=false;
							this.getData()
							this.$refs[name].resetFields();
						})
					}
					
					
				} else {
					this.$Message.error('表单验证失败!');
				}
			})
		},
		handleReset (name) {
			this.$refs[name].resetFields();
		},
		adduploat(){
			console.log(111);
			if(!(this.currentNode&&this.currentNode.length>0)){
				this.$Message.error('请选择要上传的类别!');
				return false
			}
		}
	},
	created(){
		this.getData();
	}
}